import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture(0)
Ptime = 0

mpFaceDetection = mp.solutions.face_detection
mpDraw = mp.solutions.drawing_utils
faceDetection = mpFaceDetection.FaceDetection()

while True:
    ret,frame = cap.read()
    frameRGB = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    results = faceDetection.process(frameRGB)
    if results.detections:
        for id,detection in enumerate(results.detections):
            # mpDraw.draw_landmarks(frame,detection)
            h,w,c = frame.shape
            boxC = detection.location_data.relative_bounding_box
            box = int(boxC.xmin*w), int(boxC.ymin*h), int(boxC.width*w), int(boxC.height*h)
            cv2.rectangle(frame,box,(0,0,255),2)
            cv2.putText(frame, f'{int(detection.score[0]*100)}%', (box[0], box[1]-20), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 3)

    Ctime = time.time()
    fps = 1 / (Ctime-Ptime)
    Ptime = Ctime
    cv2.putText(frame,f'fps:{int(fps)}',(10,70),cv2.FONT_HERSHEY_PLAIN,3,(0,255,0),3)
    cv2.imshow('frame',frame)
    if cv2.waitKey(30)==ord('q'):
        break
cap.release()
cv2.destroyAllWindows()